package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizTenant;
import com.member.shop.service.ISysBizTenantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【站点信息】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2025-01-08
 **/
@RestController
@RequestMapping("/shop/tenant")
public class SysBizTenantController extends BaseController {
    @Resource
    private ISysBizTenantService sysBizTenantService;

    /**
     * 查询站点信息列表
     */
    @PreAuthorize("@ss.hasPermi('shop:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizTenant sysBizTenant) {
        startPage();
        List<SysBizTenant> list = sysBizTenantService.selectSysBizTenantList(sysBizTenant);
        return getDataTable(list);
    }

    /**
     * 导出站点信息列表
     */
    @PreAuthorize("@ss.hasPermi('shop:tenant:export')")
    @Log(title = "站点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizTenant sysBizTenant) {

        List<SysBizTenant> list = sysBizTenantService.selectSysBizTenantList(sysBizTenant);
        ExcelUtil<SysBizTenant> util = new ExcelUtil<SysBizTenant>(SysBizTenant.class);
        util.exportExcel(response, list, "站点信息数据");
    }

    /**
     * 获取站点信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:tenant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizTenantService.selectSysBizTenantById(id));
    }

    /**
     * 新增站点信息
     */
    @PreAuthorize("@ss.hasPermi('shop:tenant:add')")
    @Log(title = "站点信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizTenant sysBizTenant) {
        if (sysBizTenant.getGrounding() == 1 && sysBizTenant.getIsDefault() == 1) {
            return AjaxResult.error("下架站点不能设置默认");
        }

        return toAjax(sysBizTenantService.insertSysBizTenant(sysBizTenant));
    }

    /**
     * 修改站点信息
     */
    @PreAuthorize("@ss.hasPermi('shop:tenant:edit')")
    @Log(title = "站点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizTenant sysBizTenant) {
        if (sysBizTenant.getGrounding() == 1 && sysBizTenant.getIsDefault() == 1) {
            return AjaxResult.error("下架站点不能设置默认");
        }
        SysBizTenant sysBizTenant1 = sysBizTenantService.selectSysBizTenantById(sysBizTenant.getId());
        if (sysBizTenant1.getIsDefault() == 1 && sysBizTenant.getIsDefault() == 0) {
            return AjaxResult.error("必须要有一个默认站点");
        }
        return toAjax(sysBizTenantService.updateSysBizTenant(sysBizTenant));
    }

    /**
     * 删除站点信息
     */
    @PreAuthorize("@ss.hasPermi('shop:tenant:remove')")
    @Log(title = "站点信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        SysBizTenant byId = sysBizTenantService.getById(id);
        if (byId.getIsDefault() == 1) {
            return AjaxResult.error("默认站点不能删除");
        }
        return toAjax(sysBizTenantService.deleteSysBizTenantById(id));
    }
}
